package com.hundun.hddeal.service.lmpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hundun.hddeal.dao.PayMapper;
import com.hundun.hddeal.entity.OrderDetail;
import com.hundun.hddeal.entity.Pay;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 支付业务层
 * @author 谢
 */
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    PayMapper payMapper;

    @Override
    public OrderDetail save(OrderDetail record) {

        return null;
    }

    @Override
    public Result<PageInfo<Pay>> selectByName(Integer page, Integer size, String userNames, Date startTime, Date endTime,Byte orderType) {
        try{
            List<Pay> pays1= payMapper.selectByName(userNames,startTime,endTime,orderType);
            Double sum=0.00;
            PageHelper.startPage(page, size);
            List<Pay> pays= payMapper.selectByName(userNames,startTime,endTime,orderType);
            for (int i=0;i<pays1.size();i++){
                if(orderType==1&&pays1.get(i).getStatus()==4){
                    sum+=pays1.get(i).getMoney();
                }else if(orderType==2&&pays1.get(i).getStatus()==4){
                    sum+=pays1.get(i).getMoney();
                }
            }
            BigDecimal b = new BigDecimal(sum);
            sum = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            PageInfo<Pay> newsListPageInfo = new PageInfo<>(pays);
            return ResultUtil.success(newsListPageInfo,sum);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }

    }

}
